#include "MyWindow.h"

#include "QKeyEvent"
#include "QFileDialog"
#include "QLabel"

#include "lapackUtil.h"

#include "boost/foreach.hpp"

MyWindow::MyWindow(void)
{
	p_ui = NULL;
	p_doc = NULL;
}

MyWindow::~MyWindow(void)
{
}

void MyWindow::keyPressEvent(QKeyEvent *ev) {
	switch( ev->key() ) {
		case Qt::Key_Alt:
			break;
			
		case Qt::Key_Control:
			break;

		case Qt::Key_Shift:
			break;

		default:
			break;
	}
    ev->ignore();
}

void MyWindow::keyReleaseEvent(QKeyEvent *ev) {
	switch( ev->key() ) {
		case Qt::Key_Alt:
			break;
			
		case Qt::Key_Control:
			break;

		case Qt::Key_Shift:
			break;

		case Qt::Key_Up:
			break;

		case Qt::Key_Down:
			break;

		case Qt::Key_Left:
			break;

		case Qt::Key_Right:
			break;

		default:
			break;
	}
    ev->ignore();
}

void MyWindow::connect(Ui::MainWindow *p, QDoc* pd) {
	if ( (p==NULL) || (pd==NULL) ) {
		return;
	}

	p_ui = p;
	p_doc = pd;

	QObject::connect(p_ui->zoomin, SIGNAL(clicked()), p_ui->glwidget, SLOT(zoomIn()));
	QObject::connect(p_ui->zoomout, SIGNAL(clicked()), p_ui->glwidget, SLOT(zoomOut()));
	QObject::connect(p_ui->actionOpen, SIGNAL(triggered()), this, SLOT(onOpenFile()));

	QObject::connect(p_ui->cal_laplace, SIGNAL(clicked()), this, SLOT(on_cal_laplace()));
	QObject::connect(p_ui->mul_button, SIGNAL(clicked()), this, SLOT(on_mul()));
	
}

void MyWindow::onOpenFile() {
	QLabel *label = new QLabel("<center>FileDialog</center>");
	QString filename;

	filename = QFileDialog::getOpenFileName(this, "Open Model",
		".\\", "M Files (*.m *.txt)");

	if (filename != NULL) {
		p_doc->openFile(filename.toAscii());
		p_ui->glwidget->m_points = p_doc->m_points;
		p_ui->glwidget->updateObject();
		p_ui->glwidget->updateGL();

		
	}
}

void MyWindow::on_cal_laplace() {
	p_doc->cal_laplace();
}

void MyWindow::on_mul() {
	std::vector<double> data, pt;
	double val;
	int i;
	QString filename, tofile;

	filename = QFileDialog::getOpenFileName(this, "Open Function",
		".\\", "MAT Files (*.mat *.txt)");

	tofile = QFileDialog::getSaveFileName(this, "Save result", ".\\", "MAT Files (*.mat *.txt)");
/*
	BOOST_FOREACH(pt, p_doc->m_points) {
		data.push_back(pt[3]);
	}
	*/
	data = readDoubleArray(filename.toAscii());

	pt = p_doc->multiplyMatrix(data);

	writeDoubleArray(pt, tofile.toAscii() );

	i=0;
	BOOST_FOREACH(val, pt) {
		p_ui->glwidget->m_points[i][3] = val;
		++i;
	}
	p_ui->glwidget->updateObject();
	p_ui->glwidget->updateGL();
}